import shopping_data

from keras.api.preprocessing import sequence
from keras.src.models import Sequential
from keras.src.layers import Dense, Input, Conv2D, AveragePooling2D, Flatten, Embedding
from keras.src.optimizers import SGD
import matplotlib.pyplot as plt
from keras.src.utils import to_categorical

x_train, y_train, x_test, y_test = shopping_data.load_data()

print('x_train.shape:',x_train.shape)
print('y_train.shape:',y_train.shape)
print('x_test.shape:',x_test.shape)
print('y_test.shape:',y_test.shape)
print(x_train[0])
print(y_train[0])

vocalen, word_index = shopping_data.createWordIndex(x_train,y_train)
print('word_index:',word_index)
print('词典总词数:',vocalen)

maxlen = 25
x_train_index = shopping_data.word2Index(x_train, word_index, maxlen=maxlen)
x_test_index = shopping_data.word2Index(x_test, word_index, maxlen=maxlen)

model = Sequential()
model.add(Embedding(trainable=False, input_dim=vocalen, output_dim=300, input_length=maxlen))
model.add(Flatten())
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))
model.add(Dense(100, activation='relu'))

model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

model.fit(x_train_index, y_train, batch_size=512, epochs=200)
score, acc = model.evaluate(x_test_index, y_test)

print('Test score:', score)
print('Test accuracy:', acc)